package com.yl.nettylearning.netty.rpc.netty;

import com.yl.nettylearning.netty.rpc.provider.HelloServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author yangl
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("服务端channelRead被调用。。。。。");
        //获取客户端发送的消息，并调用服务
        System.out.println("msg=" + msg);
        //客户端在调用服务的服务时候，需要定义一个协议
        //比如，每次发送消息的时候，都必须以某个字符串开头，
//        if(msg.toString().startsWith("HelloService#")){

            System.out.println("channelRead被调用---");
            String result = new HelloServiceImpl().
                    Hello(msg.toString().substring(msg.toString().lastIndexOf("#")+1));
            ctx.writeAndFlush(result);
//        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println(cause.getMessage());
        ctx.close();
    }
}
